package cn.well.cloud.modular.service.impl.thread;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.well.cloud.base.wechat.api.impl.AppWXUser;
import cn.well.cloud.base.wechat.api.impl.TemplateMessage;
import cn.well.cloud.base.wechat.model.WXUser;
import cn.well.cloud.modular.model.UserInfo;
import cn.well.cloud.modular.service.impl.UserInfoServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 同步用户信息
 */
@Slf4j
public class SyncUserInfoThread implements Runnable{

    private UserInfoServiceImpl userInfoService = SpringContextHolder.getBean("userInfoServiceImpl");

    /**
     * 构造方法传参,要发送的用户openId集合
     */
    private  List<String> userList;
    /**
     * 构造方法传参,要发送的微信类型
     */
    private String wxType ;
    /**
     * 构造方法传参,接口调用凭证
     */
    private String token ;

    private CountDownLatch begin;

    private CountDownLatch end;

    /**
     * 模板消息工具
     */
    private TemplateMessage templateMessage = new TemplateMessage();

    /**
     * 用户工具
     */
    private AppWXUser appWXUser = new AppWXUser();

    //创建个构造函数初始化 list,和其他用到的参数
    public SyncUserInfoThread(List<String> userList, String token,String wxType, CountDownLatch begin, CountDownLatch end) {
        this.userList =userList;
        this.token = token;
        this.wxType = wxType;
        this.begin = begin;
        this.end = end;
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < userList.size(); i++) {
                String openId = userList.get(i);
                WXUser wxUser = appWXUser.getUserInfo(token,openId);
                if(wxUser !=null){

                    QueryWrapper<UserInfo> qw = new QueryWrapper<UserInfo>();
                    UserInfo userInfo =new UserInfo();
                    userInfo.setOpenId(openId);
                    qw.setEntity(userInfo);
                    userInfo = userInfoService.getOne(qw);
                    if(userInfo ==null){
                        userInfo =new UserInfo();
                        userInfo.setHeadUrl(wxUser.getHeadimgurl());
                        userInfo.setUserInfoName(wxUser.getNickname());
                        userInfo.setUnionID(wxUser.getUnionid());
                        userInfo.setOpenId(openId);
                        userInfo.setSubscribe(1);
                        userInfo.setWxType(wxType);
                        userInfoService.save(userInfo);
                    }else{
                        int subscribe = userInfo.getSubscribe();
                        if(subscribe==0){
                            userInfo.setUnionID(wxUser.getUnionid());
                            userInfo.setSubscribe(1);
                            userInfoService.updateById(userInfo);
                        }
                        String unionID = userInfo.getUnionID();
                        if(StrUtil.isEmpty(unionID)){
                            userInfo.setUnionID(wxUser.getUnionid());
                            userInfoService.updateById(userInfo);
                        }
                    }
                }
                log.info(Thread.currentThread().getName());
            }

            //执行完让线程直接进入等待
            begin.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //这里要主要了，当一个线程执行完 了计数要减一不然这个线程会被一直挂起
            // ，end.countDown()，这个方法就是直接把计数器减一的
            end.countDown();
        }
    }
}
